package com.fishery.monitor.service;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fishery.monitor.dao.BaseDao;
import com.fishery.monitor.mapper.MonitorBaseMapper;
import com.fishery.monitor.pojo.MonitorBase;
import com.fishery.monitor.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.stereotype.Service;


import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;


/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class BaseService extends ServiceImpl<MonitorBaseMapper, MonitorBase> {

	@Autowired
	private BaseDao baseDao;

	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<MonitorBase> findAll() {
		return this.list(null);
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public IPage<MonitorBase> findSearch(Map whereMap, int page, int size) {
//		Specification<Base> specification = createSpecification(whereMap);
//		PageRequest pageRequest =  PageRequest.of(page-1, size);
//		return baseDao.findAll(whereMap, pageRequest);
		Page<MonitorBase> Page = new Page<>(page,size);
		QueryWrapper<MonitorBase> wrapper = new QueryWrapper<>();
		wrapper.allEq(whereMap,false);
		return baseDao.findAll(Page,wrapper);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<MonitorBase> findSearch(Map whereMap) {
		QueryWrapper<MonitorBase> wrapper = new QueryWrapper<>();
		wrapper.allEq(whereMap,false);
		return baseDao.findAll(wrapper);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	public MonitorBase findById(String id) {
		return baseDao.findById(id);
	}

	/**
	 * 增加
	 * @param base
	 */
	public void add(MonitorBase base) {
		base.setId( idWorker.nextId()+"" );
//		Date date = new Date();
//		Timestamp timestamp = new Timestamp(date.getTime());
//		base.setCreateDate(timestamp);
		this.save(base);
	}

	/**
	 * 修改
	 * @param base
	 */
	public void update(MonitorBase base) {
		this.save(base);
	}

	/**
	 * 删除
	 * @param id
	 */
	public void deleteById(String id) {
		this.removeById(id);
	}


	public List traceMonitor(String baseId) {
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("baseId", baseId);
		wrapper.eq("statusCode", Integer.valueOf(1));
		return this.baseDao.findAll(wrapper);
	}

	public void modifyStatus(String id, String status) {
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("id", id);
		MonitorBase monitorVideo = this.findById(id);
		monitorVideo.setStatusCode(status);
		this.update(monitorVideo, wrapper);
	}

	public void upData(MonitorBase monitorBase) {
		String url = "https://open.ys7.com/api/lapp/token/get";
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/x-www-form-urlencoded");
		LinkedMultiValueMap postParameters = new LinkedMultiValueMap();
		postParameters.add("appKey", "c6fe0bfab1714f9cb05efaa3c52e5e07");
		postParameters.add("appSecret", "30baac8733d378059e4c37b37898bc94");
		HttpEntity httpEntity = new HttpEntity(postParameters, headers);
		RestTemplate client = new RestTemplate();
		JSONObject s = (JSONObject)client.postForObject(url, httpEntity, JSONObject.class, new Object[0]);

		assert s != null;

		JSONObject data = s.getJSONObject("data");
		String accessToken = data.getString("accessToken");
		monitorBase.setAccessToken(accessToken);
		String url1 = "https://open.ys7.com/ezopen/h5/iframe_se?url=ezopen://" + monitorBase.getValidateCode() + "@open.ys7.com/" + monitorBase.getDeviceSerial() + "/1.live&autoplay=0&audio=1&accessToken=" + monitorBase.getAccessToken() + "&templete=2";
		String ds = monitorBase.getDeviceSerial();
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("id", monitorBase.getId());
		MonitorBase monitorVideo = this.findById(monitorBase.getId());
		monitorVideo.setVedioUrl(url1);
		monitorVideo.setAccessToken(accessToken);
		this.update(monitorVideo, wrapper);
	}

	public void addVideo(MonitorBase monitorBase) {
		monitorBase.setId(this.idWorker.nextId() + "");
		String url = "https://open.ys7.com/api/lapp/token/get";
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/x-www-form-urlencoded");
		LinkedMultiValueMap postParameters = new LinkedMultiValueMap();
		postParameters.add("appKey", "c6fe0bfab1714f9cb05efaa3c52e5e07");
		postParameters.add("appSecret", "30baac8733d378059e4c37b37898bc94");
		HttpEntity httpEntity = new HttpEntity(postParameters, headers);
		RestTemplate client = new RestTemplate();
		JSONObject s = (JSONObject)client.postForObject(url, httpEntity, JSONObject.class, new Object[0]);

		assert s != null;

		JSONObject data = s.getJSONObject("data");
		String accessToken = data.getString("accessToken");
		monitorBase.setAccessToken(accessToken);
		String url1 = "https://open.ys7.com/ezopen/h5/iframe_se?url=ezopen://" + monitorBase.getValidateCode() + "@open.ys7.com/" + monitorBase.getDeviceSerial() + "/1.live&autoplay=0&audio=1&accessToken=" + monitorBase.getAccessToken() + "&templete=2";
		monitorBase.setVedioUrl(url1);
		this.save(monitorBase);
	}

	public void refresh(String id) {
		MonitorBase monitorBase = this.findById(id);
		String url = "https://open.ys7.com/api/lapp/token/get";
		HttpHeaders headers = new HttpHeaders();
		headers.add("Content-Type", "application/x-www-form-urlencoded");
		LinkedMultiValueMap postParameters = new LinkedMultiValueMap();
		postParameters.add("appKey", "c6fe0bfab1714f9cb05efaa3c52e5e07");
		postParameters.add("appSecret", "30baac8733d378059e4c37b37898bc94");
		HttpEntity httpEntity = new HttpEntity(postParameters, headers);
		RestTemplate client = new RestTemplate();
		JSONObject s = (JSONObject)client.postForObject(url, httpEntity, JSONObject.class, new Object[0]);

		assert s != null;

		JSONObject data = s.getJSONObject("data");
		String accessToken = data.getString("accessToken");
		monitorBase.setAccessToken(accessToken);
		String url1 = "https://open.ys7.com/ezopen/h5/iframe_se?url=ezopen://" + monitorBase.getValidateCode() + "@open.ys7.com/" + monitorBase.getDeviceSerial() + "/1.live&autoplay=0&audio=1&accessToken=" + monitorBase.getAccessToken() + "&templete=2";
		String ds = monitorBase.getDeviceSerial();
		QueryWrapper wrapper = new QueryWrapper();
		wrapper.eq("id", id);
		monitorBase.setVedioUrl(url1);
		monitorBase.setAccessToken(accessToken);
		this.update(monitorBase, wrapper);
	}


}
